﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace VirtuOS.Components
{
    class FileHandler
    {
        public static string vdir_full = VirtuOS.vdir_full;

        public static string CurrentDirectory()
        {
            string cd = Environment.CurrentDirectory;
            cd = cd.Replace(vdir_full, "/");
            cd = cd.Replace("\\\\", "");
            cd = cd.Replace("\\", "/");
            cd = cd.Replace("//", "/");
            return cd;
        }

        internal static void Rename(string file, string newname)
        {
            string cdir = CurrentDirectory();
            string nfile = String.Empty;
            if (file.StartsWith("/"))
            {
                nfile = vdir_full + file;
            }
            else
            {
                nfile = cdir + "/" + file;
            }
            if (nfile.StartsWith("/")) { nfile = vdir_full + nfile; }
            string nn = nfile.Replace(file, "") + newname;
            if ((nfile.Contains(vdir_full)) && (File.Exists(nfile)) && (!File.Exists(nn)))
            {
                try
                {
                    File.Move(nfile, nn);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
        }

        internal static void List(string dir)
        {
            string cdir = CurrentDirectory();
            string ndir = String.Empty;
            if (dir == null)
            {
                ndir = cdir;
            }
            else if (dir.StartsWith("/"))
            {
                ndir = vdir_full + dir;
            }
            else
            {
                ndir = cdir + "/" + dir;
            }
            if (ndir.StartsWith("/")) { ndir = vdir_full + ndir; }
            if ((ndir.Contains(vdir_full)) && (Directory.Exists(ndir)))
            {
                string[] folders = Directory.GetDirectories(ndir);
                foreach (string s in folders)
                {
                    string tmp = s.Replace(vdir_full, "");
                    tmp = tmp.Replace("\\", "/");
                    tmp = tmp.Replace(CurrentDirectory() + "/", "");
                    tmp = tmp.Replace("/", "");
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.WriteLine(tmp);
                    Console.ResetColor();
                }
                string[] files = Directory.GetFiles(ndir);
                foreach (string s in files)
                {
                    string tmp = s.Replace(vdir_full, "");
                    tmp = tmp.Replace("\\", "/");
                    tmp = tmp.Replace(CurrentDirectory() + "/", "");
                    tmp = tmp.Replace("/", "");
                    Console.WriteLine(tmp);
                }
            }
            else
            {
                Console.WriteLine("Directory {0} not found.", dir);
            }
        }

        internal static void MakeDir(string dir)
        {
            string cdir = CurrentDirectory();
            string ndir = String.Empty;
            if (dir.StartsWith("/"))
            {
                ndir = vdir_full + dir;
            }
            else
            {
                ndir = cdir + "/" + dir;
            }
            if (ndir.StartsWith("/")) { ndir = vdir_full + ndir; }
            if ((ndir.Contains(vdir_full)) && (!Directory.Exists(ndir)))
            {
                Directory.CreateDirectory(ndir);
            }
            else
            {
                Console.WriteLine("Directory {0} already exists or is out of bounds.", ndir);
            }
        }

        internal static void DeleteFile(string file)
        {
            string cdir = CurrentDirectory();
            string nfile = String.Empty;
            if (file.StartsWith("/"))
            {
                nfile = vdir_full + file;
            }
            else
            {
                nfile = cdir + "/" + file;
            }
            if (nfile.StartsWith("/")) { nfile = vdir_full + nfile; }
            if ((nfile.Contains(vdir_full)) && (File.Exists(nfile)))
            {
                try
                {
                    File.Delete(nfile);
                }
                catch (IOException e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            else
            {
                Console.WriteLine("File not found.");
            }
        }

        internal static void DeleteFolder(string folder, bool recursive)
        {
            string cdir = CurrentDirectory();
            string nfolder = String.Empty;
            if (folder.StartsWith("/"))
            {
                nfolder = vdir_full + folder;
            }
            else
            {
                nfolder = cdir + "/" + folder;
            }
            if (nfolder.StartsWith("/")) { nfolder = vdir_full + nfolder; }
            if ((nfolder.Contains(vdir_full)) && (Directory.Exists(nfolder)))
            {
                if (recursive == true)
                {
                    Directory.Delete(nfolder, true);
                }
                else
                {
                    string[] check = Directory.GetFiles(nfolder);
                    if (check.Length == 0)
                    {
                        Directory.Delete(nfolder, false);
                    }
                }
            }
            else
            {
                Console.WriteLine("Directory {0} not found.", folder);
            }
        }

        internal static void MakeFile(string file)
        {
            string cdir = CurrentDirectory();
            string nfile = String.Empty;
            if (file.StartsWith("/"))
            {
                nfile = vdir_full + file;
            }
            else
            {
                nfile = cdir + "/" + file;
            }
            if (nfile.StartsWith("/")) { nfile = vdir_full + nfile; }
            if ((nfile.Contains(vdir_full)) && (!File.Exists(nfile)) && (!file.Equals("/")))
            {
                using (File.Create(nfile)) { }
            }
            else
            {
                Console.WriteLine("File {0} already exists.", file);
            }
        }

        internal static long CountLinesInFile(string f)
        {
            string cdir = CurrentDirectory();
            string nfile = String.Empty;
            if (f.StartsWith("/"))
            {
                nfile = vdir_full + f;
            }
            else
            {
                nfile = cdir + "/" + f;
            }
            if (nfile.StartsWith("/")) { nfile = vdir_full + nfile; }
            if ((nfile.Contains(vdir_full)) && (File.Exists(nfile)) && (!f.Equals("/")))
            {
                long count = 0;
                using (StreamReader sr = new StreamReader(f))
                {
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        if ((line.Length > 0) && (!line.StartsWith(" ")))
                            count++;
                    }
                }
                return count;
            }
            else
            {
                return 0;
            }
        }
    }
}
